function exists = fileExists(filename,direc,option)
% This function is part of our custom made Load function for out GUI.
% It checks whether or not a file (without extension) exists [filename] in
% the specified directory [direc]. It returns a 1 for exists if the file
% is found. [option] has to be either 'decode' or 'encode'. decode mode
% finds the file and adds a .zip extension to it, while encode removes the
% .zip extension

%% Changing dir:
OldFolder = cd(direc); %Stores previously used directory, and changes direc
files = dir; %stores all info about the new directory in variable 'files'
exists = 0; %needs to be preset.

if exist('option','var') == 1
    %% Decode mode
    % adds .zip extension to the userspecified, extensionless zipfile
    if strcmp(option,'decode')
        for j = 1:length(files) %as it needs to check the whole directory
            if strcmp(filename,files(j).name)
                exists = exists + 1; % returns 1 for exists
                movefile(files(j).name,strcat(filename,'.zip')); % adds a .zip extension to file
            end
        end
        %% Encode mode
        % removes .zip extension from the savefunction created zipfile
    elseif strcmp(option,'encode')
        zipfile = strcat(filename,'.zip'); % creates a variable zipfile 
        % with a string: filename'.zip' This is actually the name of zipfile
        file = textscan(zipfile,'%s','delimiter','.');
        % splits zipfile in two strings
        movefile(zipfile,char(file{1}(1))); %takes first string from file,
        % and removes the .zip extension from the zipfile by copying it.
    end
end
cd(OldFolder); %changes current directory back to old directory
end